import os
import re
from Base.baseLogger import Logger
from Base.baseAutoHttp import ApiBase
from Base.basePath import BasePath as BP
from Base.baseExcel import ExcelWrite as EW
logger = Logger('apm_test.py').getLogger()

class VIPinsert(ApiBase):
    def __init__(self):
        super().__init__('接口元素信息')

    def insert(self,name,describe,Authorization):
        change_data = {
            "Authorization": Authorization,
            "name": name,
            "describe": describe
        }
        res = self.request_base('vip名单新增',change_data)
        return res.json()

    def assert_insert_ok(self,res,status):
        res1 = res['status']
        assert status == res1, "【断言】：断言成功，当前新增失败，失败原因为：{}".format(res1)
        logger.info("【断言】：断言成功，当前新增失败，失败原因为：{}".format(res1))

    def select(self,describe,Authorization):
        change_data = {
            "Authorization":Authorization,
            "searchVal":describe
        }
        res = self.request_base('vip名单查询',change_data)
        return res.json()

    def assert_select_ok(self,res,describe):
        res1 = res['result']['rows'][0]['rosterDescribe']
        assert describe == res1, "【断言】：断言成功，新增后的数据查询成功，VIP新增的描述为：{}".format(res1)
        logger.info("【断言】：断言成功，新增后的数据查询成功，VIP新增的描述为：{}".format(res1))

    def update(self,name,describe,busiId,rosterId,Authorization):
        change_data = {
            'name':name,
            'Describe':describe,
            'busiId':busiId,
            'rosterId' :rosterId,
            'Authorization':Authorization
        }
        res = self.request_base('vip名单修改',change_data)
        return res.json()

    def assert_update_ok(self,res):
        res1 = res['status']
        assert 'success!' == res1, "【断言】：断言成功，修改成功返回值为：{}".format(res1)
        logger.info("【断言】：断言成功，修改成功返回值为：{}".format(res1))

    def update_selete(self,describe,Authorization):
        change_data = {
            "Authorization":Authorization,
            "searchVal":describe
        }
        res = self.request_base('vip名单根据名称查询',change_data)
        return res.json()
    def assert_update_select_ok(self,res,describe):
        res1 = res['result']['rows'][0]['rosterDescribe']
        assert describe == res1, "【断言】：断言成功，修改后的数据查询成功，VIP修改的描述为：{}".format(res1)
        logger.info("【断言】：断言成功，修改后的数据查询成功，VIP修改的描述为：{}".format(res1))

    def delete(self,rosterId,Authorization):
        change_data = {
            "rosterId":rosterId,
            "Authorization":Authorization
        }
        res = self.request_base('vip名单删除',change_data)
        return res.json()

    def assert_delete(self,res):
        assert res == 1,"【断言】：断言成功，数据删除成功，删除后的返回值为：{}".format(res)
        logger.info("【断言】：断言成功，数据删除成功，删除后的返回值为：{}".format(res))

    def file_insert(self,rosterId,Authorization,filename):
        change_data = {
            "rosterId": rosterId,
            "Authorization":Authorization
        }
        filepath = os.path.join(BP.DATA_DRIVER_DIR,filename)
        files = {'file':open(filepath, 'rb')}
        res = self.request_base('vip账号导入',change_data,files=files)
        return res.json()
    def assert_file_insert(self,res):
        assert res['result'] == '导入完成:成功:3条 ,失败:0条',"【断言】：断言成功，文件导入成功：{}".format(res['result'])
        logger.info("【断言】：断言成功，文件导入成功：{}".format(res['result']))


    def vip_account(self,objId,Authorization):
        change_data = {
            "objId": objId,
            "Authorization": Authorization
        }
        res = self.request_base('vip账号点击账号数,查看账号信息', change_data)
        return res.json()
    def assert_vip_account(self,res):
        assert res['result']['rows'][0]['userAccount'] == '333', "【断言】：断言成功，导入后第三个用户的账号为：{}".format(res['result']['rows'][0]['userAccount'])
        logger.info("【断言】：断言成功，导入后第三个用户的账号为：{}".format(res['result']['rows'][0]['userAccount']))

    def download_file(self,Authorization,filename):
        change_data = {
            "Authorization": Authorization
        }
        res = self.request_base('vip账号信息下载',change_data)
        filepath = os.path.join(BP.DATA_DRIVER_DIR, filename)
        res_con = res.content
        with open(filepath, 'wb') as f:
            f.write(res_con)

    def assert_download_file(self, filename):
        '''断言文档下载'''
        path = os.path.join(BP.DATA_DRIVER_DIR, filename)
        is_exist = os.path.exists(path)
        assert is_exist, '文件不存在：{}'.format(path)
        logger.info("【断言】:文档下载验证成功,路径为：{}".format(path))

    def Excelwrite(self, list_data, excel_path):
        filepath = os.path.join(BP.DATA_DRIVER_DIR, excel_path)
        excel = EW()
        excel.excl_write(list_data,filepath)

    def assert_Excelwrite(self,excel_path):
        filepath = os.path.join(BP.DATA_DRIVER_DIR, excel_path)
        assert filepath != None,"【断言】：正在插入Excel数据，文件的路径为：{}".format(filepath)
        logger.info("【断言】：正在插入Excel数据，文件的路径为：{}".format(filepath))

    def remove(self,excel_path):
        filepath = os.path.join(BP.DATA_DRIVER_DIR, excel_path)
        os.remove(filepath)
        assert excel_path != None, "【断言】：正在删除Excel，文件的名称为：{}".format(excel_path)

if __name__ == '__main__':
    a = VIPinsert()
    Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJCSVQtTVNBIiwiaWF0IjoxNjUwNTExMDMzLCJuYmYiOjE2NTA1MDk4MzMsImV4cCI6IjI2NDEzODA2ODQiLCJjb25zdW1lciI6IkJJVC1NU0EiLCJ0eXBlIjoidXNlciIsImlkIjoicm9vdCIsIm5hbWUiOiIiLCJ0ZW5hbnRJZCI6ImFkbWluIn0.U1-giqVnX1o8xrFVoORmJOW2cW_Fwa6xr6AxA560FaY'
    filename = '自动化下载测试.xls'
    list_data = [ { '序号': 111.0, '用户名': 222.0, '账号': 333.0 },
               { '序号': 444.0, '用户名': 555.0, '账号': 666.0 },
               { '序号': 777.0, '用户名': 888.0, '账号': 999.0 } ]
    excel_path = os.path.join(BP.DATA_DRIVER_DIR, '自动化下载测试.xls')
    res = a.remove(filename)
    print(res)
